<?php

namespace app\admin\model;

/**
 * 角色缓存
 */
class RolesCache
{

    public function getRoles()
    {
        return $this->roles();
    }

    /**
     * 通过Id获取角色信息
     * @param $id
     * @return array|mixed|null
     */
    public function getById($id)
    {
        $roles = $this->roles();
        foreach ($roles as $role) {
            if ($role['id'] == $id) {
                return $role;
            }
        }
        return null;
    }

    /**
     * 通过名字获取角色信息
     * @param $roleName
     * @return array|mixed|null
     */
    public function getByName($roleName)
    {
        $roles = $this->roles();
        foreach ($roles as $role) {
            if ($role['name'] == $roleName) {
                return $role;
            }
        }
        return null;
    }

    protected function roles()
    {
        $configModel = new Config();
        $roles = $configModel->getConfigCacheByName('roles');
        if (!$roles) {
            return [];
        }
        return $roles;
    }
}